#include "mypacketmodel.h"
#include "..\MyPacketProcessor\exports.h"

MyPacketModel::MyPacketModel(QList<MyPacket*> *packList, QObject *parent) : QAbstractTableModel(parent)
{
    packetList = packList;
}

int MyPacketModel::rowCount(const QModelIndex & ) const
{
    return packetList->count();
}

int MyPacketModel::columnCount(const QModelIndex & ) const
{
    return(3);
}

QVariant MyPacketModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::TextAlignmentRole)
    {
        return int(Qt::AlignLeft | Qt::AlignVCenter);
    }

	if (role == Qt::DisplayRole)
    {
        switch (index.column())
        {
            case 0: {return index.row();}
            case 1: {return QString::fromStdString( packetList->at(index.row())->GetDirection() ); }
            case 2: {return QString::fromStdString( packetList->at(index.row())->GetPacketName() ); }
        }
    }

    return QVariant();
}

QVariant MyPacketModel::headerData(int section, Qt::Orientation  orientation ,int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    QStringList headers;
    headers << "#" << "Dir" << "Name";
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        return headers[section];
    }
    return QVariant();
}

void MyPacketModel::Update()
{
    reset();
}

